package com.dikang.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dikang.yygh.common.exception.YyghException;
import com.dikang.yygh.common.result.ResultCodeEnum;
import com.dikang.yygh.mapper.DepartmentMapper;
import com.dikang.yygh.model.hosp.Department;
import com.dikang.yygh.repository.DepartmentRepository;
import com.dikang.yygh.service.DepartmentService;
import com.dikang.yygh.vo.hosp.DepartmentQueryVo;
import com.dikang.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramrMap) {
        // 先转化为string类型然后再转换成Department类型
        String jsonString = JSONObject.toJSONString(paramrMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);
        // 判断是否存在department数据
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExist != null) {
            // 更新要传入之前的id
            department.setId(departmentExist.getId());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        } else {
            // 添加
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            department.setCreateTime(new Date());
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.DEFAULT_DIRECTION, "createTime");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;

    }

    @Override
    public void remove(String depcode, String hoscode) {
        // 根据id查询是否存在部门
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            // 物理删除
            departmentRepository.deleteById(department.getId());
        } else {
            throw new YyghException(ResultCodeEnum.FAIL);
        }

    }

    @Override
    public List<DepartmentVo> selectDepartmentByHoscode(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        List<Department> departmentList = departmentRepository.findAll(example);
        // 根据大科室编号分组
        Map<String, List<Department>> collect = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        System.out.println(collect);

        for (String depCode : collect.keySet()) {
            // 大存储科室编号、大科室名称
            // 大科室下的小科室列表
            List<Department> departmentList1 = collect.get(depCode);
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(depCode);
            departmentVo.setDepname(departmentList1.get(0).getBigname());
            // 封装小科室
            List<DepartmentVo> deptSmall = new ArrayList<>();
            for (Department department : departmentList1) {
                DepartmentVo departmentChild = new DepartmentVo();
                departmentChild.setDepname(department.getDepname());
                departmentChild.setDepcode(department.getDepcode());
                deptSmall.add(departmentChild);
            }
            departmentVo.setChildren(deptSmall);
            result.add(departmentVo);
        }
        return result;
    }

    @Override
    public String getDepName(String depcode) {
        Department department = departmentRepository.getDepartmentByDepcode(depcode);
        if (department != null){
            return  department.getDepname();
        }else{
            return "";
        }
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
